package yunjiao.javatutorials.designpatterns.interpreter;

/**
 * 非结束表达式
 *
 * @author yangyunjiao
 */
public abstract class NonTerminalExpression implements Expression {
    protected final Expression expr1;
    protected final Expression expr2;

    public NonTerminalExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public static class AndExpression extends NonTerminalExpression {

        public AndExpression(Expression expr1, Expression expr2) {
            super(expr1, expr2);
        }

        @Override
        public boolean interpreter(String s) {
            return expr1.interpreter(s) && expr2.interpreter(s);
        }

        @Override
        public String toString() {
            return "and";
        }
    }

    public static class OrExpression extends NonTerminalExpression {

        public OrExpression(Expression expr1, Expression expr2) {
            super(expr1, expr2);
        }

        @Override
        public boolean interpreter(String s) {
            return expr1.interpreter(s) || expr2.interpreter(s);
        }

        @Override
        public String toString() {
            return "or";
        }
    }
}
